﻿using System;
using System.Diagnostics;

namespace DeviceUnion.FFmpeg.WinForm
{
    public class FFmpegRecorder : IDisposable
    {
        internal Process Handle { get; set; }
        public event EventHandler<FFmpeg.WinForm.LogEventArgs> OnCmdOutput;

        public FFmpegRecorder()
        {
            Handle = new Process();
            Handle.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
            Handle.StartInfo.EnvironmentVariables.Add("ffmpeg", System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ffmpeg.exe"));
            Handle.StartInfo.FileName = "cmd";
            Handle.StartInfo.Verb = "-runas";
            Handle.StartInfo.RedirectStandardInput = true;
            Handle.StartInfo.RedirectStandardOutput = true;
            Handle.StartInfo.RedirectStandardError = true;
            Handle.StartInfo.UseShellExecute = false;
            Handle.StartInfo.CreateNoWindow = true;
            Handle.OutputDataReceived -= Handle_OutputDataReceived;
            Handle.OutputDataReceived += Handle_OutputDataReceived;
            Handle.ErrorDataReceived -= Handle_OutputDataReceived;
            Handle.ErrorDataReceived += Handle_OutputDataReceived;
            
        }

        internal void Play(string args = "ffmpeg -y -f gdigrab  -t 30 -r 25 -i desktop -vcodec libx264 -s 1280x720 -cropleft 1366x768 test720.mp4")
        {
            Handle.StandardInput.WriteLine(args); 
        }

        public void RecordDesktop(string flieName, int rate = 6)
        {
            var cmd = string.Format("ffmpeg -y -f gdigrab -t 5 -r 10 -video_size 1920x1080 -offset_x 0 -offset_y 0 -framerate 6 -i desktop -vcodec libx264 ");
            cmd += string.Format(" -vf drawtext=\"fontfile = msyh.ttc:x = 100:fontcolor = red:fontsize = 30:text='{0}'\"", Environment.TickCount.ToString());
            cmd += " " + flieName;
            Handle.StandardInput.WriteLine(cmd);
        }

        public void RecordWindow(string window, string file, int rate = 25)
        {
            //var cmd = string.Format("ffmpeg -y -f gdigrab -i title={0} -framerate {1} -vcodec libx264 {2} ", window, rate, file);
            var cmd = string.Format("ffmpeg -y -f gdigrab -i title={0} -framerate {1} -vcodec libx264 -draw_mouse 0", window, rate);
            //cmd += " -vf drawtext=\"fontfile = msyh.ttc:x = w - tw:fontcolor = red:fontsize = 30:text = '%{localtime\\:%H-\\:%M-\\:%S}'\"";
            //cmd += " -vf drawtext=\"x = w - tw:fontcolor = red:fontsize = 30:text = '%{localtime\\:%H-\\:%M-\\:%S}'\"";
            cmd += " -vf drawtext=\"fontfile = msyh.ttc:x = w - tw:fontcolor = red:fontsize = 30:text = 'Hello World!!!'\"";
            cmd += " " + file;
            Handle.StandardInput.WriteLine(cmd);
        }

        public void RecordWindowEx(string window, string file, string wstr = "%{localtime\\:%y-\\:%m-\\:%d \\:%H-\\:%M-\\:%S}")
        {
            var cmd = string.Format("ffmpeg -y -f gdigrab -t 20 -i title={0} -framerate {1} -r 10 -draw_mouse 1 -vcodec libx264 -metadata ok='HellWorld' ", window, 15);
            cmd += string.Format(" -vf drawtext=\"fontfile = msyh.ttc:x = w - tw:fontcolor = red:fontsize = 30:text='{0}'\"", wstr);
            cmd += " " + file;
            Handle.StandardInput.WriteLine(cmd);
        }

        public void Stop()
        {
            Handle.StandardInput.WriteLine("q");
        }

        private void Handle_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            OnCmdOutput?.Invoke(this, new FFmpeg.WinForm.LogEventArgs(e.Data));
        }

        public void StartCmd()
        {
            Handle.Start();
            Handle.BeginOutputReadLine();
            Handle.BeginErrorReadLine();
        }

        public void WriteCmd(string cmd)
        {
            Handle.StandardInput.WriteLine(cmd);
        }

        public void Dispose()
        {
            if (!Handle.HasExited)
                Handle.Kill();
        }
    }


}
